
jQuery('AA');
//type aliases  类型别名

type sumType = (x: number, y: number) => number;  //将类型抽象出来
function sum(x: number, y: number): number {
  return x + y;
}
const sum2: sumType = sum;
console.log(sum2(1, 2));


type nameResolver = (name:string) => string
function getName(n: string | nameResolver) :string {
  if(typeof n == 'string') {
    return n
  } else {
    return n('mouddong');
  }
}

console.log(getName('str'));



// type assertion 类型断言

type numberResolver = string | number;

function numberResolvers(input: numberResolver): number {
  //第一种类型断言 写法
  // const str = input as String;
  // if (str.length) {
  //   return str.length
  // } else {
  //   const num = input as Number;
  //   return num.toString().length;
  // }


  //第二种类型断言写法
  if ((<string>input).length) {
    return (<string>input).length
  } else {
    return input.toString().length;
  }

}
